# encoding: utf-8
require "activerecord-import"
# 供应商控制器
# * 对供应商对象进行持久话
# * 接收供应商发来的GPS数据
# * 提供远程调用接口，持久化gps数据
class SuppliersController < ApplicationController
  include SuppliersHelper
  # GET /supplies
  # GET /supplies.json
  def index
    @suppliers = Supplier.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @suppliers }
    end
  end

  # GET /supplies/1
  # GET /supplies/1.json
  def show
    @supplier = Supplier.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @supplier }
    end
  end

  # GET /supplies/new
  # GET /supplies/new.json
  def new
    @supplier = Supplier.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @supplier }
    end
  end

  # GET /supplies/1/edit
  def edit
    @supplier = Supplier.find(params[:id])
  end

  # POST /supplies
  # POST /supplies.json
  def create
    @supplier = Supplier.new(params[:supply])

    respond_to do |format|
      if @supplier.save
        format.html { redirect_to @supplier, notice: 'Supplier was successfully created.' }
        format.json { render json: @supplier, status: :created, location: @supplier }
      else
        format.html { render action: "new" }
        format.json { render json: @supplier.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /supplies/1
  # PUT /supplies/1.json
  def update
    @supplier = Supplier.find(params[:id])

    respond_to do |format|
      if @supplier.update_attributes(params[:supply])
        format.html { redirect_to @supplier, notice: 'Supplier was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @supplier.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /supplies/1
  # DELETE /supplies/1.json
  def destroy
    @supplier = Supplier.find(params[:id])
    @supplier.destroy

    respond_to do |format|
      format.html { redirect_to supplies_url }
      format.json { head :ok }
    end
  end

  # 接收GPS数据
  # POST /suppliers/1/receive_gps_data
  def receive_gps_data
    @supplier = Supplier.find(params[:id])
    if @supplier.gps_data?(params[:data], params[:timestamp], params[:sign])
      logger.info "gps_data :  #{params[:data]} ....."
      logger.info "timestamp :  #{params[:timestamp]} ....."
      logger.info "sign :  #{params[:sign]} ....."
      gps_data_map = read_gps(params[:data])
      logger.info "gps_data_map:--=--=-#{gps_data_map}"
      @supplier.dispatch gps_data_map
      render :text => '200'
      #head :ok
    else
      head :error
    end
  end

  #持久化gps数据
  #@param【:gps_data】:gps数据，json格式
  def persist_gps_data
    begin
      gps = ActiveSupport::JSON.decode params[:gps_data_array]
      logger.info "suppliers_controller::persist_gps_data===>>>---#{gps}"
      columns = [:vehicle_no, :time, :latitude, :longitude, :speed, :direction]
      GpsHistory.import columns, gps
        #GpsLatest.import columns, gps
        #update_gps_latest
    rescue Exception=> e
      logger.error e.message
      render :text => '500'
    end
    render :text => '200'
  end

  #暂时无用了
  #将最新的车辆gps数据持久化到gps_latest表中
  def update_gps_latest
    puts "==========method   update_gps_latest===>>>>>>"
    begin
      mc = Opal::Mc::Mc.new('VEHICLE_MC')
      gps_latest_list = []
      vehicle_list = mc.get('vehicle_list')
      puts "vehicle_list::---#{vehicle_list}"
      if vehicle_list
        vehicle_list.each do |vehicle_no|
          gps_data = mc.get(vehicle_no)
          data = [vehicle_no, gps_data.datetime, gps_data.lat, gps_data.lng, gps_data.speed, gps_data.direction]
          gps_latest_list << data
        end

        puts "update_gps_latest===>>>===#{gps_latest_list}"
        GpsLatest.where(:vehicle_no => vehicle_list).delete_all()
        logger.info "update_gps_latest===>>>===#{gps_latest_list}"
        columns = [:vehicle_no, :time, :latitude, :longitude, :speed, :direction]
        GpsLatest.import columns, gps_latest_list

        mc.delete('vehicle_list')
      end
      puts "<<<<<<<<<<===method  update_gps_latest===OVER========"
    rescue Exception => e
      logger.error e.message
    end
  end


  skip_before_filter :require_login, :only => [:receive_gps_data, :persist_gps_data, :update_gps_latest]
end
